﻿using Comm;
using Kinlo.Model;
using Kinlo.Repo;
using Kinlo.UI.Main.VM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// RepairInputView.xaml 的交互逻辑
    /// </summary>
    public partial class RepairInputView : Window
    {
        AfterWeightFirstRep DB = new AfterWeightFirstRep();
        public RepairInputView()
        {
            InitializeComponent();
            this.DataContext = new RepairInputVM(this);
            //AfterWeights = new ObservableCollection<AfterWeightFirst>();
            //this.datagrid1.ItemsSource = AfterWeights;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (barcode.Text.Contains("\r") || barcode.Text.Contains("\n"))
            { 
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

                    if (elementWithFocus != null)
                    {
                        elementWithFocus.MoveFocus(request);
                    }
                    e.Handled = true; 
            }
        }
        AfterWeightFirst batt = null;
        /// <summary>
        /// 查找 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                batt = DB.GetAfterWeightByBarcode(barcode.Text.Trim());
                if (batt != null)
                {
                    fWeight.Text = batt.BeforWeight_One.ToString();
                    bWeight.Text = batt.AfterWeight_One.ToString();
                    injWeight.Text = batt.InjectionNum_One.ToString();
                }
                else
                {
                    MessageBox.Show(string.Format("没有找到对应该的电池信息 {0}", barcode.Text.Trim()), "错误");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                if (batt != null)
                {
                    if (rWeight.Value > 10)
                    {
                        batt.SupplementWeight_One = rWeight.Value;
                        batt.SupplementNum_One = batt.SupplementWeight_One - batt.AfterWeight_One;
                        batt.ISSuppled_One = "True";
                        batt.ProductDate = DateTime.Now;
                        batt.IsRead = 0;
                        var injVal = batt.SupplementWeight_One - batt.BeforWeight_One;
                        if (injVal >= GlobalValues.InjValMin && injVal <= GlobalValues.InjValMax)
                        {
                            batt.InjectionNum_Offset_One = null;
                            batt.Result_One = "True";
                        }
                        else
                        {
                            batt.InjectionNum_Offset_One = injVal-GlobalValues.InjValStd;
                            batt.Result_One = "False";
                        }
                        var count = DB.UpdateAfterWeightFirst(batt);
                        if (count > 0)
                        {
                            UpdateAfterWeightsData(batt);
                            var msg = string.Format("保存成功 {0} {1}", batt.Code, batt.SupplementWeight_One);
                            Logger.Info(msg);
                            MessageBox.Show(msg, "信息");
                            batt = null;
                            barcode.Text = "";
                            rWeight.Value = 0;
                            fWeight.Text = "";
                            bWeight.Text = "";
                            injWeight.Text = "";
                        }
                        else
                        {
                            MessageBox.Show(string.Format("保存失败 {0} {1}", batt.Code, batt.SupplementWeight_One), "信息");
                        }

                    }
                    else
                    {
                        MessageBox.Show(string.Format("数据异常请检查 {0} {1}", batt.Code, rWeight.Value), "错误");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        void UpdateAfterWeightsData(AfterWeightFirst w)
        {
            try
            {
                var ww = AfterWeights.Where(s => s.Code == w.Code).FirstOrDefault();
                if (ww != null)
                {
                    ww.ProductDate = w.ProductDate;
                    ww.Result_One = w.Result_One;
                    ww.ISSuppled_One = w.ISSuppled_One;
                    ww.SupplementNum_One = w.SupplementNum_One;
                    ww.SupplementWeight_One = w.SupplementWeight_One;
                    ww.InjectionNum_Offset_One = w.InjectionNum_Offset_One;
                }
                else
                {
                    AfterWeights.Add(w);
                }
                //this.datagrid1.ItemsSource = null;
                //this.datagrid1.ItemsSource = AfterWeights;

                //var ww = AfterWeights.Where(s => s.Code == w.Code);
                //if (ww != null)
                //{
                //    foreach (var w1 in ww)
                //    {
                //        AfterWeights.Remove(w1);
                //    }
                //}
                //    AfterWeights.Add(w);  
             }
            catch(Exception ex) { 
            }

        }

        private void barcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            { 
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                 
                UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                 
                if (elementWithFocus != null)
                {
                    elementWithFocus.MoveFocus(request);
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        private void barcode_LostFocus(object sender, RoutedEventArgs e)
        {
            Button_Click(null,null);
        }

        ObservableCollection<AfterWeightFirst> afterWeights;
        /// <summary>
        /// 补液后称重
        /// </summary>
        public ObservableCollection<AfterWeightFirst> AfterWeights
        {
            get { return afterWeights; }
            set
            {
                afterWeights = value; 
            }
        }
    }
}
